package br.com.gbvbahia.mybooks.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.commons.modelos.EntityInterface;
import br.com.gbvbahia.commons.utils.AndroidUtils;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.dao.PrateleiraDAO;
import br.com.gbvbahia.mybooks.modelos.PrateleiraVO;

public class PrateleiraCadastrarFragment extends FragmentMae {

	private Button btnCadastro;
	private Button btnCancel;
	private Button btnNew;
	private EditText inputNome;
	private EditText inputDescricao;
	private CheckBox checkBox;
	private PrateleiraVO pratCad;
	private PrateleiraDAO dao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dao = new PrateleiraDAO(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.frag_prat_edit, null);
		btnCadastro = (Button) view.findViewById(R.id.btnHeaderOk);
		btnCancel = (Button) view.findViewById(R.id.btnHeaderCancelar);
		btnNew = (Button) view.findViewById(R.id.btnHeaderNovo);
		inputNome = (EditText) view.findViewById(R.id.inputPratNome);
		inputDescricao = (EditText) view.findViewById(R.id.inputDescricao);
		checkBox = (CheckBox) view.findViewById(R.id.inputPublica);
		setPrateleiraCad(null);
		btnCadastro.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pratCad = getPrateleira();
				if (TextUtils.isEmpty(pratCad.getNome())) {
					AndroidUtils.alertDialog(
							PrateleiraCadastrarFragment.this.getActivity(),
							R.string.aviso_35, R.string.app_name);
					return;
				}
				if(!pratCad.seValidar().isEmpty()){
					for(Integer aviso : pratCad.seValidar()){
						Toast.makeText(getActivity(),
								getString(aviso),
								Toast.LENGTH_SHORT).show();
					}
					return;
				}
				startTransacao(new SalvarPrateleiraTransacao(pratCad));
				inputNome.requestFocus();
				AndroidUtils.closeVirtualKeyboard(getActivity(), inputNome);
			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getActivity().finish();
			}
		});

		btnNew.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				setPrateleiraCad(null);
			}
		});
		return view;
	}

	public void setPrateleiraCad(PrateleiraVO prateleira) {
		if (prateleira != null) {
			this.inputNome.setText(prateleira.getNome());
			this.inputDescricao.setText(prateleira.getDescricao());
			if(prateleira.getPublica() == EntityInterface.TRUE){
				checkBox.setChecked(true);
			}else {
				checkBox.setChecked(false);
			}
			this.pratCad = prateleira;
		} else {
			this.inputDescricao.setText("");
			this.inputNome.setText("");
			pratCad = new PrateleiraVO();
			if(pratCad.getPublica() == EntityInterface.TRUE){
				checkBox.setChecked(true);
			}else {
				checkBox.setChecked(false);
			}
		}
	}

	private PrateleiraVO getPrateleira() {
		if (pratCad == null) {
			pratCad = new PrateleiraVO();
		}
		pratCad.setNome(inputNome.getText().toString());
		pratCad.setDescricao(inputDescricao.getText().toString());
		pratCad.setPublica(checkBox.isChecked() ? EntityInterface.TRUE : EntityInterface.FALSE);
		return pratCad;
	}

	/**
	 * Transacao para salvar livro de modo assincrono.
	 * 
	 * @author Guilherme
	 * 
	 */
	public class SalvarPrateleiraTransacao implements Transacao {
		private PrateleiraVO toSave;
		private long idPrat;

		public SalvarPrateleiraTransacao(PrateleiraVO livro) {
			super();
			toSave = livro;
		}

		@Override
		public void executar() throws Exception {
			if (toSave.getId() == null) {
				idPrat = dao.insertPrepare(toSave);
			} else {
				idPrat = dao.updatePrepare(toSave);
			}
		}

		@Override
		public void atualizarView() {
			if (getActivity() != null && !getActivity().isFinishing())
				if (idPrat > 0) {
					Toast.makeText(
							PrateleiraCadastrarFragment.this.getActivity(),
							toSave.getNome() + " "
									+ getString(R.string.aviso_0),
							Toast.LENGTH_SHORT).show();
					setPrateleiraCad(null);
					FragmentTransfer tf = (FragmentTransfer) PrateleiraCadastrarFragment.this
							.getActivity();
					tf.updateFragment(R.layout.frag_prat_edit, idPrat);

				} else {
					AndroidUtils.alertDialog(PrateleiraCadastrarFragment.this
							.getActivity(), PrateleiraCadastrarFragment.this
							.getActivity().getString(R.string.aviso_34),
							R.string.app_name);
				}
		}

	}
}
